package data;

import java.sql.*;

public class Insert {
    //新增一个翻译员,返回该翻译员的主键
    public static int profile(){
        int pwd=(int)(Math.random()*100000000+100000000);
        String sql="INSERT INTO translator(pwd) VALUES (?);";
        Connection connection=DatabaseManage.getConnection();

        try {
            PreparedStatement st=connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            st.setString(1,pwd+"");
            st.executeUpdate();

            ResultSet rs=st.getGeneratedKeys();
            rs.next();
            int id=rs.getInt(1);

            rs.close();
            st.close();
            return id;
        }catch (SQLException ex){
            ex.printStackTrace();
            return -1;
        }
    }

    //新增一个城市
    public static boolean city(String city){
        String sql="INSERT INTO city(name) VALUES (?);";

        String[] strings={city};

        return updata(sql,strings);
    }

    //新增一个语言
    public static boolean language(String language){
        String sql="INSERT INTO language(name) VALUES (?);";

        String[] strings={language};

        return updata(sql,strings);
    }

    //新增一个gallery
    public static boolean gallery(String tid,String img){
        String sql="INSERT INTO gallery(tid, img) VALUES (?,?);";

        String[] strings={tid,img};

        return updata(sql,strings);
    }

    //新增一个review
    public static boolean review(String tid,String title,String content){
        String sql="INSERT INTO review(tid,title,content) VALUES (?,?,?);";

        String[] strings={tid,title,content};

        return updata(sql,strings);
    }

    //更新数据处理
    static boolean updata(String sql, String[] content){
        Connection connection=DatabaseManage.getConnection();

        try {
            PreparedStatement st=connection.prepareStatement(sql);
            for(int i=0;i<content.length;i++){
                st.setString(i+1,content[i]);
            }

            if(st.executeUpdate()==0){
                st.close();
                return false;
            }else {
                st.close();
                return true;
            }
        }catch (SQLException ex){
            ex.printStackTrace();
            return false;
        }finally {
        }
    }
}
